<?php

class AdminCategory extends Admin{

	public $useDB = array("Category");
	public $autoRender = false;

	public function index($page=1,$sortby="name",$sortdir="asc",$parent=null){
		$page = (int)$page>0 ? (int)$page : 1;;

		$this->set("total",$this->dbCategory->count());
		$this->set("page",$page);
		$this->set("sortby",$sortby);
		$this->set("sortdir",$sortdir);
		$this->set("categories", $this->dbCategory->search(false, "$sortby $sortdir", $page, $parent));

		$this->render("admin/category/index");
	}
	
	public function rename($id, $name){
		$saved = $this->dbCategory->exec("UPDATE categories SET name='".mysql_real_escape_string($name)."' WHERE id=$id");
		if(!$saved){
			header("HTTP/1.0 500 Error saving category");
		}else{
			echo "Izmene su sačuvane.";
		}
	}
	
	public function reorder(){
		$order = $_POST["order"];
		
		$saved = false;
		foreach($order as $id=>$position){
			$q = "UPDATE categories SET position=$position WHERE id=$id LIMIT 1";
			$s = $this->dbCategory->exec($q);
			$saved = $saved || $s;
		}
		
		if($saved){
			$this->flashSet("Novi redosled je uspešno sačuvan.", "ok");
		}else{
			$this->flashSet("Greška", "error");
		}
	}
	
	public function add($name, $parent_id=0){
		$exist = (int)$this->dbCategory->field("id", array("conditions"=>array("name"=>$name, "parent_id"=>$parent_id)));
		if($exist){
			header("HTTP/1.0 500 Error");
			echo "Kategorija \"$name\" već postoji. (ID=$exist)";
		}else{
			$pos = 1 + (int)$this->dbCategory->field("position", array("order"=>"position DESC", "conditions"=>array("parent_id"=>$parent_id)));
			$saved = $this->dbCategory->exec("INSERT INTO categories(name,parent_id,position) VALUES('".mysql_real_escape_string($name)."', $parent_id, $pos)");
			if($saved){
				$this->flashSet("Kategorija je uspešno sačuvana", "ok");
			}else{
				header("HTTP/1.0 500 Error");
				echo "GREŠKA! Kategorija nije sačuvana!<br />".mysql_error();
			}
		}
	}
	
	public function delete($id){
		$ids = array_merge(array($id), $this->_children($id));
		
		$saved = true;
		foreach($ids as $id){
			$name = $this->dbCategory->field("name", array("conditions"=>array("id"=>$id)));
			$filename1 = DIR_IMAGE . "/category/" . urlize($name) . ".jpg";
			$filename2 = DIR_IMAGE . "/category/" . urlize($name) . "_h.jpg";
			
			$saved = $saved && $this->dbCategory->exec("DELETE c.*, pc.* FROM categories c LEFT JOIN product_categories pc ON c.id=pc.category_id WHERE c.id=$id");
			if($saved){
				unlink($filename1);
				unlink($filename2);
			}else{
				break;
			}
		}
		
		if($saved){
			$this->flashSet("Kategorija #$id i sve njene podkategorije su uspešno obrisane.", "ok");
		}else{
			header("HTTP/1.0 500 Error");
			echo "GREŠKA! Kategorija nije obrisana.";
		}
	}
	private function _children($id){
		$ret = array();
		$c = $this->dbCategory->find(array(
			"select" => "id",
			"conditions" => array("parent_id"=>$id)
		));
		if($c){
			foreach($c as $cat){
				$ret[] = $cat["id"];
				if($this->dbCategory->count(array("conditions"=>array("parent_id"=>$cat["id"])))){
					$ret = array_merge($ret, $this->_children($cat["id"]));
				}
			}
		}
		return $ret;
	}
	
	public function change_image(){
		$file = $_FILES["image"];
		$category = $this->dbCategory->row(array("conditions"=>array("id"=>$_POST["id"])));
		
		if($category && !$file["error"]){
			$filename = urlize($category["name"]);
			$path = DIR_IMAGE . "/category/{$filename}_h.jpg";
			$path_bw = DIR_IMAGE . "/category/$filename.jpg";
			if(move_uploaded_file($file["tmp_name"], $path)){
				$img = new Imagick($path);
				$img->setImageFormat("jpg");
				if($img->getImageWidth()!=220 && $img->getImageHeight()!=180){
					$img->scaleimage(220, 180, false);
				}
				$img->writeImage($path);
				
				$img->modulateImage(100,0,0);
				$img->writeImage($path_bw);
			}
		}
		
		redirect("/admin_category");
	}
}